/*ball.js
this ball.js send and receive every tick
*/
//ball
function ball(pong){
	this.x = 0;
	this.y = 0;
	this.width = 10;
	this.height = 10;
	this.dx = 0;
	this.dy = 0;
	this.speed = 5;
	var t;
	var buffer = new Array();

	this.update = function(msg){
		if(msg[0] == 'pickup'){
			//kann man noch umziehen eventuel
			pong.addPickup(msg[2]);
			return;
		}
		buffer.push(msg);
		if(pong.loopMode == 'buffering' && buffer.length > 20)pong.loopMode = 'client';

	};
	this.serverTick = function(){
		var newX = this.x + this.dx*this.speed;
		var newY = this.y + this.dy*this.speed;
		
		if( newX >= (pong.width/2) && this.dx > 0 ){//checke auf server ende
			if(newX > (pong.width/2) ){
				//genau auf linie mit richtigem y setzten
				var diff = Math.abs((pong.width/2)-this.x);
				newY = (this.y + (this.dy*diff));
				newX = pong.width/2;
			}
			pong.socket.send('ball', pong.enemy, new Array(newX,newY,pong.loopCount,this.dx,this.dy,1));
			//direction
			this.dx = 0;
			this.dy = 0;
			pong.loopMode = 'buffering';
			buffer = new Array();
			console.log('to client : ');
		}else{//normaler server tick
			if(this.coll(newX, newY)){//pixelgenau collidieren ... return new pos(genau am object)
				newX = this.x;
				newY = this.y;
			}			
			pong.socket.send('ball', pong.enemy, new Array(newX,newY,pong.loopCount));
		}
		this.x = newX;
		this.y = newY;
		this.checkGoal();
	};
	this.clientTick = function(){
		
		if(pong.loopMode == 'client'){
			if(buffer[0][2][2]<= pong.loopCount){
				var msg = buffer[0];
				var data = buffer[0][2];
				if(msg[0] =='goal'){
					pong.entities[1].points = data[1];
					pong.entities[0].points = data[0];
					if(pong.entities[1].points == 10 || pong.entities[0].points == 10){
						pong.exit();
						return;
					}
					this.startRound(data[3]);
				}else{
					this.x = ( pong.width - data[0] );//x
					this.y = data[1];//y
					if(data[5] ){
						//set to server
						pong.loopMode = 'server';
						console.log('to server : ');
						pong.addPickup();
						this.setDirection(data[3]*-1, data[4]);
						buffer = new Array();
					}
				}
				buffer.shift();//shift() // kill first array element
				if(buffer.length == 0 && pong.loopMode == 'client'){
					pong.loopMode = 'buffering';
				}
			}
		}else{//buffering
			//console.log('buffering')
			//@todo eventuel WAIT ausgabe 
		}
	};
	this.setDirection = function(dx, dy){
		this.dx = dx;
		this.dy = dy;
	};
	this.checkGoal = function(){
		var  point = false;
		if( (this.x+this.width)< 0 ){
			point = pong.entities[1];
		}
		if( this.x+this.width> pong.width ){
			point = pong.entities[0];
		}
		if(point){
			pong.event.fire('onGoal', null);
			point.addPoint();
			//send points
			var loopMoode = 'client';
			if(pong.entities[0].points >= pong.entities[1].points){
				loopMoode = 'server';
				pong.socket.send('goal',pong.enemy,new Array(pong.entities[1].points,pong.entities[0].points,pong.loopCount,'client'));
			}else{
				pong.socket.send('goal',pong.enemy,new Array(pong.entities[1].points,pong.entities[0].points,pong.loopCount,'server'));
			}
			if(pong.entities[1].points == 10 || pong.entities[0].points == 10){
				//eventuel noch ne feuerwerks und regen annimation im pixel style
				//switch2shell frame (gieb match ausgang mit)
				pong.exit();
				return;
			}
			this.startRound(loopMoode);
		}
	};
	this.startRound = function(loopMode){
		pong.loopMode = 'buffering';
		this.x = (pong.width/2);
		this.y = (pong.height/2);
		if(loopMode == 'server'){
			this.dx = 0;
			this.dy = 0;
			pong.loopMode = 'server';
			t = setTimeout(function(){
					pong.entities[2].setDirection(-1,Math.random()-0.5);
			},1000);
		}else{
			buffer = new Array();
		}
	};

	this.coll = function(X,Y){
		//entiti check
		//this ist in einer jayQuery :) each immer das value ---frag wenn du des nich checkst
		//deswegen speichern wir this in ner extra var
		var ball = this;

		$(pong.entities).each(function(key, entiti){
			if(entiti == ball)return true;
			//rechts
		    if( (X + ball.width/2) <= (entiti.x - entiti.width/2) )
		    	return true;
			//links
			if( (X - ball.width/2) >= (entiti.x + entiti.width/2) )
				return true;
			//unten
		    if( (Y + ball.height/2) <= (entiti.y - entiti.height/2) )
		    	return true;
			//oben
		    if( (Y - ball.height/2) >= (entiti.y + entiti.height/2))
		    	return true;
		    
		    //on collidate

		    //@todo ?? pixelgenau abfragen-> ball drehen if(parallel){ abprallen }
			if(entiti instanceof wall){
				ball.setDirection(ball.dx,(ball.dy*-1));
				pong.event.fire('onColl', new Array(ball, entiti));
			}
			
			if(entiti instanceof player || entiti instanceof enemy){
				var posFromMiddle = ball.y - entiti.y;
				ball.speed++;
				ball.setDirection((ball.dx*-1),(ball.dy+(posFromMiddle/30)));
				pong.event.fire('onColl', new Array(ball, entiti));
			}
			
			if(entiti.instaceof == 'pickup'){
				entiti.start();
			}
			
			return false;

		});

	};
}